﻿using System;
using System.Data;
using System.Data.SqlClient;
using Common;

[assembly: CLSCompliant(true)]
namespace DataAccess
{
    public class PostDao
    {
        private SqlConnection _sqlConnection;
        public PostDao()
        {
            _sqlConnection = SqlTools.GetConnection();
        }

        /// <summary>
        /// Views all posts.
        /// </summary>
        /// <returns></returns>
        public DataTable ViewAllPost()
        {
            try
            {
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.ViewAllPost);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Reads the A post.
        /// </summary>
        /// <param name="postId">The post id.</param>
        /// <returns></returns>
        public DataTable ReadAPost(int postId)
        {
            try
            {
                var parameter = new SqlParameter("@PostId", SqlDbType.Int) { Value = postId };
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.ReadAPost, parameter);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// Views the post by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public DataTable ViewPostByUserId(int userId)
        {
            try
            {
                var parameter = new SqlParameter("@UserId", SqlDbType.Int) {Value = userId};
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.ViewPostByUserId, parameter);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Searches the post.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <returns></returns>
        public DataTable SearchPost(string title)
        {
            try
            {
                var parameter = new SqlParameter("@Title", SqlDbType.Text) {Value = title};
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.SearchPost, parameter);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Deletes the post.
        /// </summary>
        /// <param name="postId">The post id.</param>
        /// <returns></returns>
        public int DeletePost(int postId)
        {
            try
            {
                var parameter = new SqlParameter("@PostId", SqlDbType.Int) {Value = postId};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.DeletePost, parameter);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Deletes the post by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public int DeletePostByUserId(int userId)
        {
            try
            {
                var parameter = new SqlParameter("@UserId", SqlDbType.Int) {Value = userId};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.DeletePostByUserId,
                                                 parameter);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Creates the post.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="shortDesc">The short description.</param>
        /// <param name="content">The content.</param>
        /// <param name="image">The image link.</param>
        /// <param name="date">The date.</param>
        /// <param name="status">The status.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="category">The category id.</param>
        /// <returns></returns>
        public int CreatePost(string title, string shortDescription, string content, 
            string image, DateTime date, int status, int userId, int category)
        {
            try
            {
                var parameters = new SqlParameter[8];
                parameters[0] = new SqlParameter("@Title", SqlDbType.Text) { Value = title };
                parameters[1] = new SqlParameter("@ShortDescription", SqlDbType.Text) { Value = shortDescription };
                parameters[2] = new SqlParameter("@Content", SqlDbType.Text) { Value = content };
                parameters[3] = new SqlParameter("@Image", SqlDbType.Text) { Value = image };
                parameters[4] = new SqlParameter("@DatePost", SqlDbType.DateTime) { Value = date };
                parameters[5] = new SqlParameter("@Status", SqlDbType.Bit) { Value = status };
                parameters[6] = new SqlParameter("@UserId", SqlDbType.Int) { Value = userId };
                parameters[7] = new SqlParameter("@CateId", SqlDbType.Int) { Value = category };
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.CreatePost, parameters);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Edits the post.
        /// </summary>
        /// <param name="postId">The post id.</param>
        /// <param name="title">The title.</param>
        /// <param name="shortDescription">The short description.</param>
        /// <param name="content">The content.</param>
        /// <param name="imageLink">The image link.</param>
        /// <param name="datePost">The date post.</param>
        /// <param name="status">The status.</param>
        /// <param name="cateId">The category id.</param>
        /// <returns></returns>
        public int EditPost(int postId, string title, string shortDescription, string content, 
            string imageLink, DateTime datePost, int status, int categoryId)
        {
            try
            {
                var parameters = new SqlParameter[8];
                parameters[0] = new SqlParameter("@PostId", SqlDbType.Int) { Value = postId };
                parameters[1] = new SqlParameter("@Title", SqlDbType.Text) { Value = title };
                parameters[2] = new SqlParameter("@ShortDescription", SqlDbType.Text) { Value = shortDescription };
                parameters[3] = new SqlParameter("@Content", SqlDbType.Text) { Value = content };
                parameters[4] = new SqlParameter("@ImageLink", SqlDbType.Text) { Value = imageLink };
                parameters[5] = new SqlParameter("@DatePost", SqlDbType.DateTime) { Value = datePost };
                parameters[6] = new SqlParameter("@Status", SqlDbType.Bit) { Value = status };
                parameters[7] = new SqlParameter("@CateId", SqlDbType.Int) { Value = categoryId };
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.EditPost);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// Updates the post views.
        /// </summary>
        /// <param name="postId">The post id.</param>
        /// <returns></returns>
        public int UpdatePostViews(int postId)
        {
            try
            {
                var parameter = new SqlParameter("@PostId", SqlDbType.Int) {Value = postId};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.UpdateViews, parameter);
            }
            catch (Exception)
            {
                
                throw;
            }
        }


    }
}
